package ca.etsmtl.log430.lab3;

import java.util.*;

public class ListStudentsRegistered extends Communication implements Runnable {
	boolean stop = false;

	public ListStudentsRegistered(Integer RegistrationNumber,
			String ComponentName) {
		super(RegistrationNumber, ComponentName);

	} // Constructor

	@Override
	public void update(Observable Thing, Object NotificationNumber) {
		Menus Menu = new Menus();
		Displays Display = new Displays();
		Course MyCourse = new Course();

		// The update() method is an abstract method that is called whenever the
		// notifyObservers() method is called by the Observable class
		// First we check to see if the NotificationNumber is equal to this
		// thread's RegistrationNumber or SYSTEM_SHUT_DOWN. If they are, then we
		// execute - otherwise, we stay idle.

		if (registrationNumber.compareTo((Integer) NotificationNumber) == 0) {
			addToReceiverList("ListCoursesComponent");

			signalReceivers("ListCoursesComponent");

			// Next we ask them to pick a course

			MyCourse = Menu.pickCourse(CommonData.theListOfCourses
					.getListOfCourses());

			if (MyCourse != null) {
				// If the course is valid (exists in the list), them we display
				// the
				// students that are stored in the course object's internal list

				Display.displayStudentsRegistered(MyCourse);

			} else {

				System.out.println("\n\n *** Course not found ***");

			} // if

		} // if

		if (SYSTEM_SHUT_DOWN_ID.compareTo((Integer) NotificationNumber) == 0) {
			stop = true;

		} // if

		removeFromReceiverList("ListCoursesComponent");

	} // update

	public void run() {
		System.out.println("\nListCourses Component Started...");

		try {
			while (!stop) {
				Thread.sleep(1000);

			} // while

			System.out.println("\nListCourses Component Shutdown...");

		} catch (Exception e) {

			System.out.println("Exception occured in ListCourses.run(): " + e);

		} // try-catch

	} // run

} // ListStudentsRegistered